﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;
namespace jnlamp.Web.lampinfo
{
    /// <summary>
    /// GetScd 的摘要说明
    /// </summary>
    public class GetScd : IHttpHandler, IRequiresSessionState
    {
        jnlamp.BLL.scdInfo bll = new jnlamp.BLL.scdInfo();

        public void ProcessRequest(HttpContext context)
        {

            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);
            else if (ac == "queryb")//通过基地查找灯的当前状态
                Queryb(context);
            else if (ac == "querybc")//通过基地查找灯的当前状态
                Querybc(context);
            else if (ac == "delete")
                delete(context);
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["lid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querybc(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string bid = "";
                string status = "";
                //if (null != context.Request.Form["status"])
                //{//获取前台传来的值  
                //    status = context.Request.Form["status"].ToString().Trim();
                //}
                if (null != context.Request.Form["bid"])
                {//获取前台传来的值  
                    bid = context.Request.Form["bid"].ToString().Trim();
                }
                if (null != context.Request.QueryString["bid"])
                {//获取前台传来的值  
                    bid = context.Request.QueryString["bid"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 1000;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();

                if (bid != "")
                {
                    if (bid.IndexOf(",") == -1)
                        strWhere.AppendFormat(" bid={0} and ", bid);
                    else
                    {
                        string[] ids = bid.Split(',');
                        StringBuilder sb = new StringBuilder();
                        sb.Append("(");
                        foreach (string idm in ids)
                        {
                            sb.AppendFormat(" bid={0} or ", idm);
                        }
                        int mstartindex = sb.ToString().LastIndexOf("or");//获取最后一个and的位置  
                        if (mstartindex >= 0)
                        {
                            sb.Remove(mstartindex, 2);//删除多余的and关键字  
                        }

                        sb.Append(")");
                        strWhere.Append(sb.ToString());
                    }

                }
                if (status != "" && status != "0")
                {
                    strWhere.AppendFormat(" lampstatus={0} and ", status);
                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = new DataSet();//   bll.GetListByPageBase(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                DataTable dt = new DataTable();
                ds.Tables.Add(dt);
                int count = bll.GetRecordCountBase(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Queryb(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string bid = "";
                string status = "";
                if (null != context.Request.Form["status"])
                {//获取前台传来的值  
                    status = context.Request.Form["status"].ToString().Trim();
                }
                if (null != context.Request.Form["bid"])
                {//获取前台传来的值  
                    bid = context.Request.Form["bid"].ToString().Trim();
                }
                if (null != context.Request.QueryString["bid"])
                {//获取前台传来的值  
                    bid = context.Request.QueryString["bid"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 1000;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();

                if (bid != "")
                {
                    if (bid.IndexOf(",") == -1)
                        strWhere.AppendFormat(" bid={0} and ", bid);
                    else
                    {
                        string[] ids = bid.Split(',');
                        StringBuilder sb = new StringBuilder();
                        sb.Append("(");
                        foreach (string idm in ids)
                        {
                            sb.AppendFormat(" bid={0} or ", idm);
                        }
                        int mstartindex = sb.ToString().LastIndexOf("or");//获取最后一个and的位置  
                        if (mstartindex >= 0)
                        {
                            sb.Remove(mstartindex, 2);//删除多余的and关键字  
                        }

                        sb.Append(")");
                        strWhere.Append(sb.ToString());
                    }

                }
                if (status != "" && status != "0")
                {
                    strWhere.AppendFormat(" lampstatus={0} and ", status);
                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPageBase(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCountBase(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string gwid = "", lampid = "";
                //获取前台传来的值 
                string StartTime = "", EndTime = "";

                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }

                if (null != context.Request.Form["gwid"])
                {//获取前台传来的值  
                    gwid = context.Request.Form["gwid"].ToString().Trim();
                }
                if (null != context.Request.Form["lampid"])
                {//获取前台传来的值  
                    lampid = context.Request.Form["lampid"].ToString().Trim();
                }
                //================================================================  
                //获取分页和排序信息：页大小，页码，排序方式，排序字段  
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }
                StringBuilder strWhere = new StringBuilder();
                if (StartTime != "")
                {
                    strWhere.AppendFormat(" inputTime>='{0}' and ", StartTime);
                }
                if (EndTime != "")
                {
                    strWhere.AppendFormat(" inputTime<='{0}' and ", EndTime);
                }
                if (gwid != "")
                {
                    strWhere.AppendFormat(" gprs_id='{0}' and ", gwid);

                }
                if (lampid != "")
                {
                    strWhere.AppendFormat(" scd_id='{0}' and ", lampid);

                }
                //strWhere.AppendFormat(" uid={0} and ", uid);
                //删除多余的and  
                int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                if (startindex >= 0)
                {
                    strWhere.Remove(startindex, 3);//删除多余的and关键字  
                }
                if (sort != "" && order != "")
                {
                    oderby = order + " " + sort;
                }
                DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                context.Response.Write(strJson);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}